package com.primeton.fickviewtest;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.EventType;
import com.espertech.esper.core.context.util.AgentInstanceViewFactoryChainContext;
import com.espertech.esper.core.service.EPStatementHandleCallback;
import com.espertech.esper.event.EventAdapterService;
import com.espertech.esper.view.CloneableView;
import com.espertech.esper.view.View;
import com.espertech.esper.view.ViewSupport;

public class CountView extends ViewSupport implements CloneableView {

	private final AgentInstanceViewFactoryChainContext	agentInstanceViewFactoryContext;

	private final Integer																hour;

	private final Integer																minute;

	private EPStatementHandleCallback										handle;

	private Long																				cutoffTimestampMinute;

	private Long																				currentDay;

	private static int																	count						= 0;

	private final EventBean[]														eventsPerStream	= new EventBean[1];

	public CountView(AgentInstanceViewFactoryChainContext agentInstanceViewFactoryContext, Integer hour, Integer minute) {
		super();
		this.agentInstanceViewFactoryContext = agentInstanceViewFactoryContext;
		this.hour = hour;
		this.minute = minute;
	}

	@Override
	public void update(EventBean[] newData, EventBean[] oldData) {
		// TODO Auto-generated method stub
		if (newData == null) {
			return;
		}
		for (EventBean event : newData) {
			eventsPerStream[0] = event;

			Long nowDay = getNowDay();
			if (currentDay == null || nowDay > currentDay) {
				currentDay = nowDay;
				initialCount();
			}
			count++;
			postData();
		}

	}

	private void postData() {
		// TODO Auto-generated method stub
		System.out.println("postData");
		System.out.println("目前数量:" + count);
		CountValue countV = new CountValue(count, eventsPerStream[0]);
		EventBean outgoing = agentInstanceViewFactoryContext.getStatementContext().getEventAdapterService()
				.adapterForBean(countV);
		updateChildren(new EventBean[] { outgoing }, null);
	}

	private void initialCount() {
		// TODO Auto-generated method stub
		count = 0;
		System.out.println("initialCount");
	}

	private Long getNowDay() {
		// TODO Auto-generated method stub
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		return cal.getTimeInMillis();
	}

	private Long getCurrentDay() {
		// TODO Auto-generated method stub
		if (currentDay == null) {

		}
		return currentDay;
	}

	@Override
	public EventType getEventType() {
		// TODO Auto-generated method stub
		return getEventType(agentInstanceViewFactoryContext.getStatementContext().getEventAdapterService());
	}

	@Override
	public Iterator<EventBean> iterator() {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException("Not supported");
	}

	@Override
	public View cloneView() {
		// TODO Auto-generated method stub
		return new CountView(agentInstanceViewFactoryContext, hour, minute);
	}

	protected static EventType getEventType(EventAdapterService eventAdapterService) {
		return eventAdapterService.addBeanType(CountValue.class.getName(), CountValue.class, false, false, false);
	}

}
